#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
struct people
{
	string m_name;
	char m_gender;
	int m_age;
	string m_phone;
	string m_addr;
};
struct contacts
{
	struct people person[MAX];
	int m_size;
};
void showMeun()
{
	cout<<"1.add number"<<endl;
	cout<<"2.show number"<<endl;
	cout<<"3.delete number"<<endl;
	cout<<"4.find number"<<endl;
	cout<<"5.update number"<<endl;
	cout<<"6.clear number"<<endl;
	cout<<"0.exit"<<endl;
}
void addPerson(contacts *con)
{
	if(con->m_size == MAX)
	{
		cout<<"contacts have full, can not addition"<<endl;
		return;
	}
	else
	{
		string name;
		cout<<"please input name:";
		cin>>name;
		con->person[con->m_size].m_name = name;
		char gender;
		do
		{
			cout<<"please input gender[m/f]:";
			cin>>gender;
			con->person[con->m_size].m_gender = gender;
		}while(gender != 'm' && gender != 'f');
		int age;
		cout<<"please input age:";
		cin>>age;
		con->person[con->m_size].m_age = age;
		string phone;
		cout<<"please input phone:";
		cin>>phone;
		con->person[con->m_size].m_phone = phone;
		string addr;
		cout<<"please input addr:";
		cin>>addr;
		con->person[con->m_size].m_addr = addr;
		// update size
		con->m_size++;
		cout<<"add succeeded"<<endl;
		system("pause");
		system("cls");
	}
}
void showPeople(const contacts * const con)
{
	if(con->m_size == 0)
	{
		cout<<"contacts have nothing"<<endl;
	}	
	else
	{
		for(int i = 0; i < con->m_size; ++i)
		{
			cout<<"name:"<<con->person[i].m_name<<"\t";
			cout<<"gender:"<<(con->person[i].m_gender == 'm' ? "male":"female")<<"\t";
			cout<<"age:"<<con->person[i].m_age<<"\t";
			cout<<"phone:"<<con->person[i].m_phone<<"\t";
			cout<<"address:"<<con->person[i].m_addr<<"\t"<<endl;
		}
	}
	system("pause");
	system("cls");
}
int isExist(contacts const * const con, string name)
{
	for (int i = 0; i < con->m_size; ++i)
	{
		if(con->person[i].m_name == name)
		{
			return i;			
		}
	}
	return -1;
}
void deletePerson(contacts * con)
{
	cout<<"please input name:";
	string name;
	cin>>name;
	int ret = isExist(con, name);
	if(ret != -1)
	{
		for (int i = ret; i < con->m_size; ++i)
		{
			// data move
			con->person[i] = con->person[i+1];
		}
		con->m_size--;
		cout<<"delete succeeded"<<endl;
	}
	else
	{
		cout<<"name is not exist"<<endl;
	}
}
void findPerson(contacts * con)
{
	cout<<"please input name:";
	string name;
	cin>>name;
	int ret = isExist(con, name);
	if(ret != -1)
	{
		cout<<"find succeeded"<<endl;
		cout<<"name:"<<con->person[ret].m_name<<"\t";
		cout<<"gender:"<<(con->person[ret].m_gender == 'm' ? "male":"female")<<"\t";
		cout<<"age:"<<con->person[ret].m_age<<"\t";
		cout<<"phone:"<<con->person[ret].m_phone<<"\t";
		cout<<"address:"<<con->person[ret].m_addr<<"\t"<<endl;
	}
	else
	{
		cout<<"find fail"<<endl;
	}
	system("pause");
	system("cls");
}
void modifyPerson(contacts * con)
{
	cout<<"please input name:";
	string name;
	cin>>name;
	int ret = isExist(con, name);
	if(ret != -1)
	{
		// name
		cout<<"please input name:";
		string name;
		cin>>name;
		con->person[ret].m_name = name;
		// gender
		cout<<"please input gender:"<<endl;
		char gender;
		do
		{
			cout<<"please f or m:";
			cin>>gender;
		}while(gender != 'm' && gender != 'f');
		con->person[ret].m_gender = gender;
		// age
		cout<<"please input age:";
		int age;
		cin>>age;
		con->person[ret].m_age = age;
		// phone
		cout<<"please input phone:";
		string phone;
		cin>>phone;
		con->person[ret].m_phone = phone;
		// addr
		cout<<"please input addr:";
		string addr;
		cin>>addr;
		con->person[ret].m_addr = addr;
		cout<<"modify succeeded"<<endl;
	}
	else
	{
		cout<<"find fail"<<endl;
	}
	system("pause");
	system("cls");
}
void cleanPerson(contacts * con)
{
	con->m_size = 0;
	cout<<"cleared"<<endl;
	system("pause");
	system("cls");
}
int main()
{
	contacts abs;
	abs.m_size = 0;
	int select = 0;
	do
	{
		showMeun();
		cin>>select;
		switch(select)
		{
			case 1: addPerson(&abs); //1.add number
				break;
			case 2: showPeople(&abs); //2.show number
				break;
			case 3: //3.delete number
				// {
				// 		cout<<"please input name:"; 
				// 		string name;
				// 		cin>>name;
				// 		if(isExist(&abs, name) == -1)
				// 			cout<<"null in the contacts"<<endl;
				// 		else
				// 			cout<<"succeeded"<<endl;
				// }
				deletePerson(&abs);
				break;
			case 4: findPerson(&abs); //4.find number
				break;
			case 5: modifyPerson(&abs); //5.update number
				break;
			case 6: //6.clear number
				break;
			case 0: //0.exit
				cout<<"see you next time"<<endl;
				break;
			default:
				break;
		}
	}while(0<select);
	system("pause");
	return 0;
}